package com.jie.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.entity.Airport;
import com.jie.mapper.AirportMapper;
import com.jie.service.AirportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小杰说java
 * @since 2021-05-19
 */
@Service
public class AirportServiceImpl extends ServiceImpl<AirportMapper, Airport> implements AirportService {

    @Autowired
    AirportService asc;
    @Autowired
    AirportMapper mapper;
    @Cacheable(value = "page",key = "'limit'")
    @Override
    public R limit(Long page, Long limit) {
        Page<Airport> pageParam = new Page<>(page,limit);
        asc.page(pageParam,null);
        long total = pageParam.getTotal();
        List<Airport> records = pageParam.getRecords();
        return  R.ok().data("total", total).data("rows", records);
    }
    @Cacheable(value = "get",key = "'getPuer'")
    @Override
    public List<Map<String, Object>> getPuer() {
        //旅客总量
        Map<String, Object> volume = mapper.TotalPassengerVolume();
      //航班总量
        Map<String, Object> ofFlights = mapper.getTotalNumberOfFlights();
        //每个月的y基准价
        Map<String, Object> average = mapper.getAverage();
        //
        Map<String, Object> numberPositions = mapper.getTotalNumberPositions();
        List<Map<String, Object>> maps=new ArrayList<>();
           maps.add(volume);
        maps.add(ofFlights);
        maps.add(average);
        maps.add(numberPositions);

         return  maps;
    }
    @Cacheable(value = "get",key = "'getEryi'")
    @Override
    public List<Map<String, Object>> getEryi() {
        // 每月旅客量
        Map<String, Object> monthlyPassengerVolume = mapper.getMonthlyPassengerVolume();
        //月航班总量
        Map<String, Object> monthlyFlights = mapper.getTotalMonthlyFlights();
        //每个月的y基准价
        Map<String, Object> average = mapper.getAverage();
       //每月主航位总量
        Map<String, Object> monthlyTotalNumber = mapper.getMonthlyTotalNumber();
        List<Map<String, Object>> maps=new ArrayList<>();
          maps.add(monthlyPassengerVolume);
          maps.add(monthlyFlights);
          maps.add(average);
          maps.add(monthlyTotalNumber);
          return maps;
    }
    @Cacheable(value = "get",key = "'getMonthlyPassenger'")
    @Override
    public List<Map<String, Object>> getMonthlyPassenger() {
        //5到10月的每个月游客量
        Map<String, Object> map = mapper.getMonthlyPassengerVolume();
        List<Map<String, Object>> maps=new ArrayList<>();
        maps.add(map);
        return  maps;

    }
    @Cacheable(value = "get",key = "'getTotalMFlights'")
    @Override
    public List<Map<String, Object>> getTotalMFlights() {
        //航班每月数量
        Map<String, Object> monthlyFlights = mapper.getTotalMonthlyFlights();
        List<Map<String, Object>> maps=new ArrayList<>();
        maps.add(monthlyFlights);
        return maps;

    }
    //前十
    @Override
    @Cacheable(value = "get",key = "'getTopCities'")
    public List<Map<String, Double>> getTopCitiess() {
        List<Map<String, Double>> list = mapper.getTopHotCities();
        return list;
    }

    @Override
    @Cacheable(value = "get",key = "'getTopCitiess'")
    public List<List<Map<String, Object>>> getTopCities(Integer cont) {
        List<Map<String, Object>> popularCityNames = mapper.getPopularCityNames(cont);
        List<Map<String, Object>> cityFrequentFlyers = mapper.getCityFrequentFlyers(cont);
        List<Map<String, Object>> citySharePassenger = mapper.getCitySharePassenger(cont);
        List<List<Map<String, Object>>>list=new ArrayList<>();
         list.add(popularCityNames);
         list.add(cityFrequentFlyers);
         list.add(citySharePassenger);
        return list;
    }


    @Override
    @Cacheable(value = "get",key = "'getStatisticsMonthlyFlights'")
    public List<Map<String, Object>> getStatisticsMonthlyFlights() {
        Map<String, Object> monthlyFlights = mapper.getTotalMonthlyFlights();
        Map<String, Object> monthlyPassengerVolume = mapper.getMonthlyPassengerVolume();
        List<Map<String, Object>> maps=new ArrayList<>();
        maps.add(monthlyPassengerVolume);
        maps.add(monthlyFlights);
        return maps;
    }
    @Cacheable(value = "get",key = "'getTotalNumber'")
    @Override
    public List<Map<String, Object>> getTotalNumber() {
        Map<String, Object> monthlyFlights = mapper.getTotalMonthlyFlights();
        Map<String, Object> monthlyPassengerVolume = mapper.getMonthlyPassengerVolume();
        Map<String, Object> averageIncomeSharing = mapper.getAverageIncomeSharing();
        List<Map<String, Object>> maps=new ArrayList<>();
        maps.add(monthlyPassengerVolume);
        maps.add(monthlyFlights);
        maps.add(averageIncomeSharing);
        return maps;

    }

    @Cacheable(value = "get",key = "'get'")
    @Override
    public List<Map<String, Object>> get() {
        Map<String, Object> monthlyFlights = mapper.getTotalMonthlyFlights();
        Map<String, Object> average = mapper.getAverage();
        Map<String, Object> averageIncomeSharing = mapper.getAverageIncomeSharing();
        Map<String, Object> fltRteCd = mapper.getFltRteCd();
        List<Map<String, Object>> maps=new ArrayList<>();
        maps.add(monthlyFlights);
        maps.add(average);
        maps.add(averageIncomeSharing);
        maps.add(fltRteCd);
        return maps;
    }

    /**
     * 某天 某周 每个月 每年的数据
     * @return
     */
    @Cacheable(value = "get",key = "'getFlightData'")
    @Override
    public List<Map> getFlightData() {
        Map<String, Long> dayfltNbr = mapper.getDayfltNbr();
        Map<String, Long> weeklyPassenger = mapper.getWeeklyPassenger();
        Map<String, Long> monthlyPassenger = mapper.getMonthlyPassenger();
        Map<String, Object> totalNumberOfFlights = mapper.getTotalNumberOfFlights();
        List<Map> list=new LinkedList<>();
        list.add(dayfltNbr);
        list.add(weeklyPassenger);
        list.add(monthlyPassenger);
        list.add(totalNumberOfFlights);
        return list;
    }

    /**
     * 每个月的客座率
     * @return
     */
    @Cacheable(value = "get",key = "'getPassengerFive'")
    @Override
    public List<Map<String,Object>> getPassengerFive() {
        //五月
        Map<String, Object> passengerFive = mapper.getPassengerFive();
        //六月
        Map<String, Object> passengerSix = mapper.getPassengerSix();
        //七月
        Map<String, Object> passengerSeven = mapper.getPassengerSeven();
        //八月
        Map<String, Object> passengerEight = mapper.getPassengerEight();
        //九月
        Map<String, Object> passengerNine = mapper.getPassengerNine();
        //十月
        Map<String, Object> passengerTen = mapper.getPassengerTen();
        List<Map<String,Object>> list=new LinkedList<>();
        list.add(passengerFive);
        list.add(passengerSix);
        list.add(passengerSeven);
        list.add(passengerEight);
        list.add(passengerNine);
        list.add(passengerTen);

        return list;
    }

    /**
     * 自定义的接口
     * @return
     */
    @Override
    @Cacheable(value = "get",key = "'getCustomXY'")
    public List<Map<String, Object>> getCustomXY() {
        // 每月旅客量
        Map<String, Object> monthlyPassengerVolume = mapper.getMonthlyPassengerVolume();
        //每月航班总量
        Map<String, Object> monthlyFlights = mapper.getTotalMonthlyFlights();
        //每个月的y基准价
        Map<String, Object> average = mapper.getAverage();
        //每月主航位总量
        Map<String, Object> monthlyTotalNumber = mapper.getMonthlyTotalNumber();
        //每月航班航线
        Map<String, Object> fltRteCd = mapper.getFltRteCd();
        //每月航班团旅客量
        Map<String,Object> gettravel = mapper.gettravel();
        //每月旅客分担收入
        Map<String, Object> averageIncomeSharing = mapper.getAverageIncomeSharing();

        List<Map<String, Object>> maps=new ArrayList<>();
        maps.add(monthlyPassengerVolume);
        maps.add(monthlyFlights);
        maps.add(average);
        maps.add(monthlyTotalNumber);
        maps.add(fltRteCd);
        maps.add(gettravel);
        maps.add(averageIncomeSharing);
        return maps;

    }

    /**
     * '航班旅客量', '主航位量', '航班航线', '团旅客量'
     * @return
     */
    @Override
    @Cacheable(value = "get",key = "'getAllFlights'")
    public List<Map<String, Object>> getAllFlights() {
        //航班总量
        Map<String, Object> totalNumberOfFlights = mapper.getTotalNumberOfFlights();
        //旅客总量
        Map<String, Object> stringObjectMap = mapper.TotalPassengerVolume();
        //主航位
        Map<String, Object> totalNumberPositions = mapper.getTotalNumberPositions();
        //航班航线
        Map<String, Object> flightRoutes = mapper.getFlightRoutes();
        //团旅客量
        Map<String, Object> gettravels = mapper.gettravels();
        List<Map<String, Object>> list=new LinkedList<>();
        list.add(stringObjectMap);
        list.add(totalNumberPositions);
        list.add(totalNumberOfFlights);
        list.add(flightRoutes);
        list.add(gettravels);
        return list;
    }

    @Override
    @Cacheable(value = "get",key = "'getSum'")
    public List<Map<String, Double>> getSum(List<List<String>> dpts) {
        QueryWrapper<Airport> airportQueryWrapper = queryWrapper(dpts);
        List<Airport> airports = this.baseMapper.selectList(airportQueryWrapper);
        List<Map<String, Double>> sums = new ArrayList<>();
        for (int i = 0; i < dpts.get(1).size(); i++) {
            if (ObjectUtil.isNotEmpty(dpts.get(1).get(i))) {
                if (dpts.get(1).get(i).equals("pax_qty")) {
                    sums.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getPaxQty()))));
                } else if (dpts.get(1).get(i).equals("fc_pax_qty")) {
                    sums.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getFcPaxQty()))));
                } else if (dpts.get(1).get(i).equals("grp_pax_qty")) {
                    sums.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getGrpPaxQty()))));
                } else if (dpts.get(1).get(i).equals("ffp_pax_qty")) {
                    sums.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getFfpPaxQty()))));
                } else if (dpts.get(1).get(i).equals("net_amt")) {
                    sums.add(airports.stream().filter(r -> r.getNetAmt() != null).collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getNetAmt()))));
                } else if (dpts.get(1).get(i).equals("y_fr_amt")) {
                    sums.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getYFrAmt()))));
                }
            }
        }
        return sums;
    }

    @Cacheable(value = "get",key = "'getAVG'")
    @Override
    public List<Map<String, Double>> getAVG(List<List<String>> dpts) {
        QueryWrapper<Airport> airportQueryWrapper = queryWrapper(dpts);
        List<Airport> airports = this.baseMapper.selectList(airportQueryWrapper);
        List<Map<String, Double>> AVGs = new ArrayList<>();
        for (int i = 0; i < dpts.get(1).size(); i++) {
            if (ObjectUtil.isNotEmpty(dpts.get(1).get(i))) {
                if (dpts.get(1).get(i).equals("pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getPaxQty())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        map.put(key, collect.get(key) / list.size());
                    }
                    AVGs.add(map);
                } else if (dpts.get(1).get(i).equals("fc_pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getFcPaxQty())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        map.put(key, collect.get(key) / list.size());
                    }
                    AVGs.add(map);
                } else if (dpts.get(1).get(i).equals("grp_pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getGrpPaxQty())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        map.put(key, collect.get(key) / list.size());
                    }
                    AVGs.add(map);
                } else if (dpts.get(1).get(i).equals("ffp_pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getFfpPaxQty())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        map.put(key, collect.get(key) / list.size());
                        break;
                    }
                    AVGs.add(map);
                } else if (dpts.get(1).get(i).equals("net_amt")) {
                    Map<String, Double> collect = airports.stream().filter(r -> r.getNetAmt() != null).collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getNetAmt())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        map.put(key, collect.get(key) / list.size());
                        break;
                    }
                    AVGs.add(map);
                } else if (dpts.get(1).get(i).equals("y_fr_amt")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getYFrAmt())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        map.put(key, collect.get(key) / list.size());
                    }
                    AVGs.add(map);
                }
            }
        }
        return  AVGs;
    }
    @Cacheable(value = "get",key = "'getMax'")
    @Override
    public List<Map<String, Double>> getMax(List<List<String>> dpts) {
        QueryWrapper<Airport> airportQueryWrapper = queryWrapper(dpts);
        List<Airport> airports = this.baseMapper.selectList(airportQueryWrapper);

        List<Map<String, Double>> Maxs = new ArrayList<>();
        for (int i = 0; i < dpts.get(1).size(); i++) {
            if (ObjectUtil.isNotEmpty(dpts.get(1).get(i))) {
                if (dpts.get(1).get(i).equals("pax_qty")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getPaxQty() > c2.getPaxQty() ? c1 : c2), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getPaxQty());
                    }
                    Maxs.add(map);
                } else if (dpts.get(1).get(i).equals("fc_pax_qty")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getFcPaxQty() > c2.getFcPaxQty() ? c1 : c2), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getFcPaxQty());
                    }
                    Maxs.add(map);
                } else if (dpts.get(1).get(i).equals("grp_pax_qty")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getGrpPaxQty() > c2.getGrpPaxQty() ? c1 : c2), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getGrpPaxQty());
                    }
                    Maxs.add(map);
                } else if (dpts.get(1).get(i).equals("ffp_pax_qty")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getFfpPaxQty() > c2.getFfpPaxQty() ? c1 : c2), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getFfpPaxQty());
                    }
                    Maxs.add(map);
                } else if (dpts.get(1).get(i).equals("net_amt")) {
                    Map<String, Airport> collect = airports.parallelStream().filter(r -> r.getNetAmt() != null).collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getNetAmt() > c2.getNetAmt() ? c1 : c2), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getNetAmt());
                    }
                    Maxs.add(map);
                } else if (dpts.get(1).get(i).equals("y_fr_amt")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getYFrAmt() > c2.getYFrAmt() ? c1 : c2), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getYFrAmt());
                    }
                    Maxs.add(map);
                }
            }
        }
        return  Maxs;
    }
    @Cacheable(value = "get",key = "'getMin'")
    @Override
    public List<Map<String, Double>> getMin(List<List<String>> dpts) {
        QueryWrapper<Airport> airportQueryWrapper = queryWrapper(dpts);
        List<Airport> airports = this.baseMapper.selectList(airportQueryWrapper);

        List<Map<String, Double>> Mins = new ArrayList<>();
        for (int i = 0; i < dpts.get(1).size(); i++) {
            if (ObjectUtil.isNotEmpty(dpts.get(1).get(i))) {
                if (dpts.get(1).get(i).equals("pax_qty")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getPaxQty() > c2.getPaxQty() ? c2 : c1), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getPaxQty());
                    }
                    Mins.add(map);
                } else if (dpts.get(1).get(i).equals("fc_pax_qty")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getFcPaxQty() > c2.getFcPaxQty() ? c2 : c1), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getFcPaxQty());
                    }
                    Mins.add(map);
                } else if (dpts.get(1).get(i).equals("grp_pax_qty")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getGrpPaxQty() > c2.getGrpPaxQty() ? c2 : c1), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getGrpPaxQty());
                    }
                    Mins.add(map);
                } else if (dpts.get(1).get(i).equals("ffp_pax_qty")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getFfpPaxQty() > c2.getFfpPaxQty() ? c2 : c1), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getFfpPaxQty());
                    }
                    Mins.add(map);
                } else if (dpts.get(1).get(i).equals("net_amt")) {
                    Map<String, Airport> collect = airports.parallelStream().filter(r -> r.getNetAmt() != null).collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getNetAmt() > c2.getNetAmt() ? c2 : c1), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getNetAmt());
                    }
                    Mins.add(map);
                } else if (dpts.get(1).get(i).equals("y_fr_amt")) {
                    Map<String, Airport> collect = airports.parallelStream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.collectingAndThen(Collectors.reducing(
                            (c1, c2) -> c1.getYFrAmt() > c2.getYFrAmt() ? c2 : c1), Optional::get)));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        map.put(key, collect.get(key).getYFrAmt());
                    }
                    Mins.add(map);
                }
            }
        }
        return Mins;
    }
    @Cacheable(value = "get",key = "'calculateVariance'")
    @Override
    public List<Map<String, Double>> calculateVariance(List<List<String>> dpts) {
        QueryWrapper<Airport> airportQueryWrapper = queryWrapper(dpts);
        List<Airport> airports = this.baseMapper.selectList(airportQueryWrapper);
        List<Map<String, Double>> Variances = new ArrayList<>();
        for (int i = 0; i < dpts.get(1).size(); i++) {
            if (ObjectUtil.isNotEmpty(dpts.get(1).get(i))) {
                if (dpts.get(1).get(i).equals("pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getPaxQty())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        Double avg = collect.get(key) / list.size();
                        final Double[] total = {0.0};
                        Map<String, Double> collect1 = list.stream().collect(Collectors.toMap(Airport::getDptCtyCd, r -> total[0] += (r.getPaxQty() - avg) * (r.getPaxQty() - avg),
                                (k1, k2) -> k1
                        ));
                        map.put(key, total[0] / list.size());
                    }
                    Variances.add(map);
                } else if (dpts.get(1).get(i).equals("fc_pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getFcPaxQty())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        Double avg = collect.get(key) / list.size();
                        final Double[] total = {0.0};
                        Map<String, Double> collect1 = list.stream().collect(Collectors.toMap(Airport::getDptCtyCd, r -> total[0] += (r.getFcPaxQty() - avg) * (r.getFcPaxQty() - avg),
                                (k1, k2) -> k1
                        ));
                        map.put(key, total[0] / list.size());
                    }
                    Variances.add(map);
                } else if (dpts.get(1).get(i).equals("grp_pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getGrpPaxQty())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        Double avg = collect.get(key) / list.size();
                        final Double total[] = {0.0};
                        list.stream().collect(Collectors.toMap(Airport::getDptCtyCd, r -> total[0] += (r.getGrpPaxQty() - avg) * (r.getGrpPaxQty() - avg),
                                (k1, k2) -> k1
                        ));
                        map.put(key, total[0] / list.size());
                    }
                    Variances.add(map);
                } else if (dpts.get(1).get(i).equals("ffp_pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getFfpPaxQty())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        Double avg = collect.get(key) / list.size();
                        final Double[] total = {0.0};
                        list.stream().collect(Collectors.toMap(Airport::getDptCtyCd, r -> total[0] += (r.getFfpPaxQty() - avg) * (r.getFcPaxQty() - avg),
                                (k1, k2) -> k1
                        ));
                        map.put(key, total[0] / list.size());
                    }
                    Variances.add(map);
                } else if (dpts.get(1).get(i).equals("net_amt")) {
                    Map<String, Double> collect = airports.stream().filter(r -> r.getNetAmt() != null).collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getNetAmt())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        Double avg = collect.get(key) / list.size();
                        final Double[] total = {0.0};
                        Map<String, Double> collect1 = list.stream().filter(r -> r.getNetAmt() != null).collect(Collectors.toMap(Airport::getDptCtyCd, r -> total[0] += (r.getNetAmt() - avg) * (r.getNetAmt() - avg),
                                (k1, k2) -> k1
                        ));
                        map.put(key, total[0] / list.size());
                    }
                    Variances.add(map);
                } else if (dpts.get(1).get(i).equals("y_fr_amt")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getYFrAmt())));
                    Map<String, Double> map = new HashMap();
                    for (String key : collect.keySet()) {
                        List<Airport> list = airports.stream().filter(r -> r.getDptCtyCd().equals(key)).collect(Collectors.toList());
                        Double avg = collect.get(key) / list.size();
                        final Double[] total = {0.0};
                        list.stream().collect(Collectors.toMap(Airport::getDptCtyCd, r -> total[0] += (r.getYFrAmt() - avg) * (r.getYFrAmt() - avg),
                                (k1, k2) -> k1
                        ));

                        map.put(key, total[0] / list.size());
                    }
                    Variances.add(map);
                }
            }
        }
        return Variances;
    }
    @Cacheable(value = "get",key = "'getCount'")
    @Override
    public List<Map<String, Long>> getCount(List<List<String>> dpts) {
        QueryWrapper<Airport> airportQueryWrapper = queryWrapper(dpts);
        List<Airport> airports = this.baseMapper.selectList(airportQueryWrapper);
        List<Map<String, Long>> Conuts = new ArrayList<>();
        for (int i = 0; i < dpts.get(1).size(); i++) {
            if (ObjectUtil.isNotEmpty(dpts.get(1).get(i))) {
                if (dpts.get(1).get(i).equals("pax_qty")) {
                    Conuts.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.counting())));
                } else if (dpts.get(1).get(i).equals("fc_pax_qty")) {
                    Conuts.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.counting())));
                } else if (dpts.get(1).get(i).equals("grp_pax_qty")) {
                    Conuts.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.counting())));
                } else if (dpts.get(1).get(i).equals("ffp_pax_qty")) {
                    Conuts.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.counting())));
                } else if (dpts.get(1).get(i).equals("net_amt")) {
                    Conuts.add(airports.stream().filter(r -> r.getNetAmt() != null).collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.counting())));
                } else if (dpts.get(1).get(i).equals("y_fr_amt")) {
                    Conuts.add(airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.counting())));
                }
            }
        }
        return Conuts;
    }
    @Cacheable(value = "get",key = "'getCluster'")
    @Override
    public List<Map<String, List>> getCluster(List<List<String>> dpts) {
        QueryWrapper<Airport> airportQueryWrapper = queryWrapper(dpts);
        List<Airport> airports = this.baseMapper.selectList(airportQueryWrapper);
        List<Map<String, List>> clusters = new ArrayList<>();
        for (int i = 0; i < dpts.get(1).size(); i++) {
            if (ObjectUtil.isNotEmpty(dpts.get(1).get(i))) {
                if (dpts.get(1).get(i).equals("pax_qty")) {
                    Map<String, List> map = new HashMap<>();
                    map.put("旅客量", airports.stream().map(r -> r.getPaxQty()).distinct().limit(50).collect(Collectors.toList()));
                    clusters.add(map);
                } else if (dpts.get(1).get(i).equals("fc_pax_qty")) {
                    Map<String, List> map = new HashMap<>();
                    map.put("主舱位", airports.stream().map(r -> r.getFcPaxQty()).distinct().limit(50).collect(Collectors.toList()));
                    clusters.add(map);
                } else if (dpts.get(1).get(i).equals("grp_pax_qty")) {
                    Map<String, List> map = new HashMap<>();
                    map.put("团队旅客量", airports.stream().map(r -> r.getGrpPaxQty()).distinct().limit(50).collect(Collectors.toList()));
                    clusters.add(map);
                } else if (dpts.get(1).get(i).equals("ffp_pax_qty")) {
                    Map<String, List> map = new HashMap<>();
                    map.put("常旅客量", airports.stream().map(r -> r.getFfpPaxQty()).distinct().limit(50).collect(Collectors.toList()));
                    clusters.add(map);
                } else if (dpts.get(1).get(i).equals("net_amt")) {
                    Map<String, List> map = new HashMap<>();
                    map.put("每旅客分摊收入", airports.stream().map(r -> r.getNetAmt()).distinct().limit(50).collect(Collectors.toList()));
                    clusters.add(map);
                } else if (dpts.get(1).get(i).equals("y_fr_amt")) {
                    Map<String, List> map = new HashMap<>();
                    map.put("Y舱基准价", airports.stream().map(r -> r.getYFrAmt()).distinct().limit(50).collect(Collectors.toList()));
                    clusters.add(map);
                }
            }
        }
        return clusters;
    }
    @Cacheable(value = "get",key = "'predict'")
    @Override
    public List<Map<String, Double>> predict(List<List<String>> dpts) {
        QueryWrapper<Airport> airportQueryWrapper = queryWrapper(dpts);
        List<Airport> airports = this.baseMapper.selectList(airportQueryWrapper);
        List<Map<String, Double>> predicts = new ArrayList<>();
        for (int i = 0; i < dpts.get(1).size(); i++) {
            if (ObjectUtil.isNotEmpty(dpts.get(1).get(i))) {
                if (dpts.get(1).get(i).equals("pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getPaxQty())));
                    Map<String, Double> doubleMap = new HashMap<>();
                    for (String key : collect.keySet()) {
                        doubleMap.put(key, collect.get(key) / 5 * Math.random());
                    }
                    System.out.println(collect);
                    predicts.add(doubleMap);
                } else if (dpts.get(1).get(i).equals("fc_pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getFcPaxQty())));
                    Map<String, Double> doubleMap = new HashMap<>();
                    for (String key : collect.keySet()) {
                        doubleMap.put(key, collect.get(key) / 5 * Math.random());
                    }
                    System.out.println(collect);
                    predicts.add(doubleMap);
                } else if (dpts.get(1).get(i).equals("grp_pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getGrpPaxQty())));
                    Map<String, Double> doubleMap = new HashMap<>();
                    for (String key : collect.keySet()) {
                        doubleMap.put(key, collect.get(key) / 5 * Math.random());
                    }
                    System.out.println(collect);
                    predicts.add(doubleMap);
                } else if (dpts.get(1).get(i).equals("ffp_pax_qty")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getFfpPaxQty())));
                    Map<String, Double> doubleMap = new HashMap<>();
                    for (String key : collect.keySet()) {
                        doubleMap.put(key, collect.get(key) / 5 * Math.random());
                    }
                    System.out.println(collect);
                    predicts.add(doubleMap);
                } else if (dpts.get(1).get(i).equals("net_amt")) {
                    Map<String, Double> collect = airports.stream().filter(r -> r.getNetAmt() != null).collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getNetAmt())));
                    Map<String, Double> doubleMap = new HashMap<>();
                    for (String key : collect.keySet()) {
                        doubleMap.put(key, collect.get(key) / 5 * Math.random());
                    }   System.out.println(collect);
                    predicts.add(doubleMap);
                } else if (dpts.get(1).get(i).equals("y_fr_amt")) {
                    Map<String, Double> collect = airports.stream().collect(Collectors.groupingBy(Airport::getDptCtyCd, Collectors.summingDouble(r -> r.getYFrAmt())));
                    Map<String, Double> doubleMap = new HashMap<>();
                    for (String key : collect.keySet()) {
                        doubleMap.put(key, collect.get(key) / 5 * Math.random());
                    }
                    System.out.println(collect);
                    predicts.add(doubleMap);
                }
            }
        }
        return predicts;
    }
    @Cacheable(value = "get",key = "'getAllDpt'")
    @Override
    public List<String> getAllDpt() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("distinct dpt_cty_cd");
        List<Airport> list = this.baseMapper.selectList(queryWrapper);
        List<String> collect = list.stream().filter(r -> ObjectUtil.isNotEmpty(r)).map(r -> r.getDptCtyCd()).limit(100).collect(Collectors.toList());
        return collect;
    }


    public QueryWrapper<Airport> queryWrapper(List<List<String>> dpts){
        QueryWrapper<Airport> queryWrapper = new QueryWrapper();
        Object flag = true;
        for (int i = 0; i < dpts.get(0).size(); i++) {
            if (flag != null && ObjectUtil.isNotEmpty(dpts.get(0).get(i))) {
                queryWrapper.lambda().eq(Airport::getDptCtyCd, dpts.get(0).get(i));
                flag = null;
                continue;
            }
            if (ObjectUtil.isNotEmpty(dpts.get(0).get(i))) {
                queryWrapper.lambda().or().eq(Airport::getDptCtyCd, dpts.get(0).get(i));
            }
        }
        return  queryWrapper;
    }

}
